package com.wnxy.admin.common.util;

import cn.hutool.core.date.*;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wnxy.admin.common.handler.OrderStatusEnum;
import com.wnxy.admin.dto.VirtualPayDto;
import com.wnxy.admin.entity.ChargingRules;
import com.wnxy.admin.entity.Order;
import com.wnxy.admin.mapper.OrderMapper;
import com.wnxy.admin.service.IChargingRulesService;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Description: 结算工具类
 * @author (^･ｪ･^)Cat~
 * @since 2023-11-29
 */
@Component
public class SettlementUtil extends ServiceImpl<OrderMapper, Order> {

   private final static String hashKey = "Order";
   private final OrderMapper orderMapper;
   private final IChargingRulesService chargingRulesServiceImpl;
   private final RedisTemplate redisTemplate;
   private final OrderUtil orderUtil;
   public SettlementUtil(OrderMapper orderMapper, IChargingRulesService chargingRulesServiceImpl, RedisTemplate redisTemplate, OrderUtil orderUtil) {
      this.orderMapper = orderMapper;
      this.chargingRulesServiceImpl = chargingRulesServiceImpl;
      this.redisTemplate = redisTemplate;
      this.orderUtil = orderUtil;
   }

   public VirtualPayDto chargeSettlement(String cardNumber) {
      Order order = orderUtil.getOrder4Redis(cardNumber);

      if (order != null) {
          order.setEndTime(new Date());

          // 【Option2-时长结算】
          if (order.getChargeDuration() == 0) {
            Long chargeDuration = DateUtil.between(order.getCreateTime(), order.getEndTime(), DateUnit.MINUTE);
            order.setChargeDuration(chargeDuration);
         }

         // 【Option2-电量结算(未满,算1.00)】
         if (order.getChargeAmount().compareTo(BigDecimal.ZERO) < 0) {
           order.setChargeAmount(BigDecimal.valueOf(1));
         }

         // Rule结算
         ChargingRules chargingRule = getChargingRule(order.getCreateTime(), order.getChargingId());
         BigDecimal amount = NumberUtil.mul(chargingRule.getUnitPrice(),order.getChargeAmount());
         amount = NumberUtil.round(amount,2);

         // 生成VirtualPayDto结算对象 -> 刷新Redis
         VirtualPayDto virtualPayDto = new VirtualPayDto();
         virtualPayDto.setChargingId(order.getChargingId());
         virtualPayDto.setChargeAmount(order.getChargeAmount());
         virtualPayDto.setAmount(amount);
         order.setAmount(amount);

         HashOperations<String, String, Order> hashOperations = redisTemplate.opsForHash();
         hashOperations.put(hashKey, order.getPaymentMethod(), order);
         return virtualPayDto;
      }
      return null;
   }

   private ChargingRules getChargingRule(Date createTime, String ruleIds) {
      List<ChargingRules> rules = chargingRulesServiceImpl.getRules(ruleIds);
      ChargingRules rule;
      if (rules == null) {
         rule = chargingRulesServiceImpl.getOne(Wrappers.lambdaQuery(ChargingRules.class)
                 .eq(ChargingRules::getRuleId, OrderStatusEnum.CHARGING_RULES_DEFAULT.getCode()));
      }
      rule = rules.get(0);
      String[] timeRangeArray = rule.getChargingPeriod().split("-");
      String startTimeString = timeRangeArray[0];
      String endTimeString = timeRangeArray[1];

      // 解析开始时间和结束时间
      String header = DateUtil.format(createTime, "yyyy-MM-dd ");
      Date startTime = DateUtil.parse(createTime + startTimeString + ":00");
      Date endTime = DateUtil.parse(createTime + endTimeString + ":00");

      // 创建时间范围对象
      DateRange dateRange = DateUtil.range(startTime, endTime, DateField.MINUTE);
      System.out.println("dateRange" + dateRange);
      // 判断时间范围【留坑:)】
      return rule;
   }

}
